<template>
  <div class="app-container">
    <Search @resetQuery="resetQuery" @handleQuery="handleQuery" :queryParams="queryParams" :showSearch="showSearch" />

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['basic:customer:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:customer:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:customer:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['basic:customer:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch="showSearch" @update:showSearch="showSearch = $event" @queryTable="getList"></right-toolbar>
    </el-row>

    <List @handleDelete="handleDelete" @handleUpdate="handleUpdate" @handleSelectionChange="handleSelectionChange" :loading="loading" :customerList="customerList" />

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户信息对话框dialog -->
    <Dialog @submitForm="submitForm" @cancelBox="cancel" :formInfo="form" :rules="rules" :title="title" :open="open" />
  </div>
</template>

<script setup name="Customer">
import { listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer } from "@/api/basic/customer";
import Dialog from "@/components/Customer/Dialog.vue";
import Search from '@/components/Customer/Search.vue';
import List from '@/components/Customer/List.vue';
import { getCurrentInstance, reactive, ref, toRefs } from 'vue';

const { proxy } = getCurrentInstance();

const customerList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {
    id: null,
    name: null,
    city: null,
    email: null,
    address: null,
    personInCharge: null,
    phone: null,
    status: null,
    isDelete: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    name: null,
    city: null,
    email: null,
    address: null,
    personInCharge: null,
    phone: null,
    createTime: null,
    updateTime: null,
    createBy: null,
  },
  rules: {
    id: [
      { required: true, message: "主键ID不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "客户名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询客户信息列表 */
function getList() {
  loading.value = true;
  listCustomer(queryParams.value).then(response => {
    customerList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    city: null,
    email: null,
    address: null,
    personInCharge: null,
    phone: null,
    status: null,
    isDelete: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null
  };
  proxy.resetForm("customerRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加客户信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getCustomer(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改客户信息";
  });
}

/** 提交按钮 */
function submitForm(dataForm) {
  if (dataForm.id !== null) {
    updateCustomer(dataForm).then(response => {
      proxy.$modal.msgSuccess("修改成功");
      open.value = false;
      getList();
    });
  } else {
    addCustomer(dataForm).then(response => {
      proxy.$modal.msgSuccess("新增成功");
      open.value = false;
      getList();
    });
  }
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm(`是否确认删除客户信息编号为"${_ids}"的数据项？`).then(() => {
    delCustomer(_ids).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    });
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('basic/customer/export', {
    ...queryParams.value
  }, `customer_${new Date().getTime()}.xlsx`);
}

getList();
</script>